/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package com.jackrain.nea.util;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.QueryEngine;
import com.jackrain.nea.web.query.SubSystemView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;

import javax.servlet.http.HttpServletRequest;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 *add by yaoj
 */
@Slf4j
public final class Utils {

    private SubSystemView subSystemView;


    public Utils(final Locale locale) {
        this(locale, ZoneId.systemDefault());
    }

    public Utils(final Locale locale, final ZoneId defaultZoneId) {
        super();
        Validate.notNull(locale, "Locale cannot be null");
        this.subSystemView = new SubSystemView();
    }

    public List getSubSystems(HttpServletRequest request, int permissionType,int sysModel){
        List lists = new ArrayList<>();
        try {
            lists = this.subSystemView.getSubSystems(request,permissionType,sysModel);
        } catch (Exception e) {
            // log.error(e.getMessage(),e);
        }finally {
            return lists;
        }
    }


    public List getTableCategories(HttpServletRequest request, int subSystemId){
        List listc = new ArrayList<>();
        try {
            listc = this.subSystemView.getTableCategories(request,subSystemId);
        } catch (Exception e) {
            // log.error(e.getMessage(),e);
        }finally {
            return listc;
        }
    }

    public JSONObject getStore(HttpServletRequest request){
        UserImpl userWeb= (UserImpl)request.getSession().getAttribute("user");
        int storeId = userWeb.getStoreId();
        JSONObject o = QueryEngine.getInstance().doQueryObject("select ID AS STOREID,STORENAME,IMAGE AS STOREIMAGE from C_STORE WHERE ID=?",new Object[]{storeId});
        return o;
    }

}
